<?
/*
Class:     CEmailList
Version:   0.01
Author:    Krivosheya V.V. (c) Sept 2004
Descript:  E-mail list manager
*/

require_once($r."_config/db.php");
require_once($r."_engine/c_sp_gate.php");


// Subscribe email statuses
define("EL_SUBSCRIBE_OK", "ok");
define("EL_SUBSCRIBE_FAIL", "fail");
define("EL_SUBSCRIBE_INVALID_EMAIL", "invalid_email");
define("EL_SUBSCRIBE_ALREADY_EXIST", "already_exists");

// UnSubscribe email statuses
define("EL_UNSUBSCRIBE_OK", "ok");
define("EL_UNSUBSCRIBE_FAIL", "fail");


class CEmailList
{
	var $table;
	
	// public (constructor)
	function CEmailList($table)
	{
		$this->table = $table;
	}
	
	//public
	function IsValidEmail($email)
	{
		return (strpos(" ".$email, "@") && strpos(" ".$email, "."));
	}

	// public	
	function RowByEmail($email)
	{
		$query = "SELECT * FROM ".$this->table." WHERE UPPER(email)=UPPER(\"$email\") ORDER BY id ASC;";
		$res = @mysql_query($query);
		$row = ( ($res && @mysql_num_rows($res) > 0) ?
			$row = @mysql_fetch_array($res) :
			false);
		@mysql_free_result($res);
		return $row;
	}
	function RowByID($id)
	{
		$query = "SELECT * FROM ".$this->table." WHERE id=$id;";
		$res = @mysql_query($query);
		$row = ( ($res && @mysql_num_rows($res) > 0) ?
			$row = @mysql_fetch_array($res) :
			false);
		@mysql_free_result($res);
		return $row;
	}

	// public	
	function UpdateRow($row)
	{
		$query = "UPDATE ".$this->table." SET ".
			"added=\"".$row["added"]."\", ".
			"email=\"".$row["email"]."\", ".
			"unsubscribe=\"".$row["unsubscribe"]."\"".
			" WHERE id=".$row["id"].";";
		$res = @mysql_query($query);
		@mysql_free_result($res);
		return true;
	}
	
	// public
	function SubscribeEmail($email)
	{
		// Check for invalid email address
		if (!$this->IsValidEmail($email)) {
			return EL_SUBSCRIBE_INVALID_EMAIL;
		}
		
		// Check that email already exists in database
		$row = $this->RowByEmail($email);
		if (is_array($row) && $row["unsubscribe"] != 1) {
			return EL_SUBSCRIBE_ALREADY_EXIST;
		}
		
		// Check if already exists
		$row = $this->RowByEmail($email);
		if ($row !== false)
		{
			if ($row["unsubscribe"] != 0)
			{
				$row["unsubscribe"] = 0;
				return $this->UpdateRow($row);
			}
			return true;
		}
		// create sql query
		$query = "INSERT INTO ".$this->table." ".
			"(added, email, unsubscribe) VALUES (".
			"\"".date("Y-m-d H:i:s")."\", \"$email\", 0)";
		// execute sql query
		$row = ( ($res = @mysql_query($query)) ?
			$this->RowByID(@mysql_insert_id()) :
			false);
		@mysql_free_result($res);
		
		// if row is array, then ok
		$ok = is_array($row) ? EL_SUBSCRIBE_OK : EL_SUBSCRIBE_FAIL;
		
		/* ----- Send email to admins about subscription ----- */
		$mail_message = 
			"Status:         ".($ok?"ok":"fail")."\n".
			"\n".
			"ID:             ".@$row["id"]."\n".
			"Date and time:  ".@$row["added"]."\n".
			"Email address:  ".@$row["email"]."\n";
			
		global $g_email_stats, $g_spg;
		$g_spg->SendEmail(array(
			"email_from" => $email,
			"email_to" => $g_email_stats,
			"email_subject" => "Site - Newsletter Subscription",
			"email_template" => $mail_message,
		));
		
		return $ok;
	}
	
	// public
	function UnSubscribeEmail($email)
	{
		// Check that email exists in database
		$row = $this->RowByEmail($email);
		if (!is_array($row)) return EL_UNSUBSCRIBE_OK;

		// Check if email already unsibscribed
		if ($row["unsubscribe"] == 1) return EL_UNSUBSCRIBE_OK;

		// Mark email as unsubscribed
		$row["unsubscribe"] = 1;

		if (! $this->UpdateRow($row)) return EL_UNSUBSCRIBE_FAIL;
		
		/* ----- Send email to user about unsubscription ----- */
		$mail_message = 
			"This e-mail was successfully removed from W3Monitor subscription list.".
			"\n";
		
		global $g_spg;
		$g_spg->SendEmail(array(
			"email_from" => "support@w3monitor.com",
			"email_to" => $email,
			"email_subject" => "Newsletter UnSubscription",
			"email_template" => $mail_message,
		));
	
		/* ----- Send email to stats about unsubscription ----- */
		$mail_message = 
			"Status:         ok\n".
			"\n".
			"ID:             ".@$row["id"]."\n".
			"Date and time:  ".date("Y-m-d H:i:s")."\n".
			"Email address:  ".@$row["email"]."\n";

		global $g_email_stats, $g_spg;
		$g_spg->SendEmail(array(
			"email_from" => $email,
			"email_to" => $g_email_stats,
			"email_subject" => "Site - Newsletter UnSubscription",
			"email_template" => $mail_message,
		));
		
		return EL_UNSUBSCRIBE_OK;
	}
}


$g_email_list = new CEmailList("subscribers");

?>